﻿using SAD_Assignment1.BLO;
using SAD_Assignment1.DAO;
using SAD_Assignment1.Shared;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAD_Assignment1
{
    public partial class HomePage : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            int page = 1;
            try { page = Int32.Parse(Request.Params["page"]); }
            catch (Exception) { }
            int maxPhoto = 10;
            try { maxPhoto = Int32.Parse(Request.Params["maxPhoto"]); }
            catch (Exception) { }
            PhotoBLO photoBLO = new PhotoBLO();
            var list = photoBLO.getNewPhotoList(page,maxPhoto);
            grid.DataSource = list;
            grid.DataBind();
            if (list.Count == 0)
            {
                Response.Redirect(Resource.Info + "?msgID=3");
            }          
        }

        protected void likeCountLB_Load(object sender, EventArgs e)
        {
            Label likeCountLB = (Label)sender;
            HiddenField hiddenID = (HiddenField)likeCountLB.Parent.FindControl("hiddenID");
            int photoID = 0;
            try { photoID = Int32.Parse(hiddenID.Value); }
            catch (Exception) { }
            LikeBLO likeBLO = new LikeBLO();            
            likeCountLB.Text = likeBLO.countLikePhoto(photoID).ToString();
        }

        protected void unlikeCountLB_Load(object sender, EventArgs e)
        {
            Label unlikeCountLB = (Label)sender;
            HiddenField hiddenID = (HiddenField)unlikeCountLB.Parent.FindControl("hiddenID");
            int photoID = 0;
            try { photoID = Int32.Parse(hiddenID.Value); }
            catch (Exception) { }
            LikeBLO likeBLO = new LikeBLO();            
            unlikeCountLB.Text = likeBLO.countUnlikePhoto(photoID).ToString();
        }

        protected void NextPageBtn_Command(object sender, CommandEventArgs e)
        {
            int page = 1;
            try { page = Int32.Parse(Request.Params["page"]); }
            catch (Exception) { }
            int maxPhoto = 10;
            try { maxPhoto = Int32.Parse(Request.Params["maxPhoto"]); }
            catch (Exception) { }
            Response.Redirect(Resource.Home + "?page=" + (page + 1));
        }
    }
}